package quiz;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Map;
import java.util.Map.Entry;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

/**
 * Servlet implementation class ProfileServlet
 */
@WebServlet("/Profile")
public class ProfileServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public ProfileServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

		DataBase db = (DataBase) request.getServletContext().getAttribute("db");
		HttpSession session = request.getSession();
		Account acct = (Account) session.getAttribute("account");
		
		int id = -5;
		if (request.getParameter("id") != null) {
			id = Integer.parseInt(request.getParameter("id"));
		}
		
		if (acct != null){
			if (id < 0 || id == acct.getID()) {
			
				request.setAttribute("announcements", db.getAnnoucements(10));
				
				// Messages
				request.setAttribute("messages", db.getUserReceivedMessages(acct.getID()));
				
				// My Achievements
				request.setAttribute("achievements", db.getUserAchievements(acct));
				
				// Account Activity - modify 10 if necessary to whatever fits best
				request.setAttribute("recentResults", db.getRecentResultsForUser(acct, 10));
				
				//My Recently-created quizzes
				request.setAttribute("recentQuizzesCreated", db.getRecentlyCreatedQuizzes(acct.getID(), 10));
				
				//Friends' recent activity
				request.setAttribute("friendsRecentResults", db.getFriendsRecentResults(acct.getID(), 6));
				request.setAttribute("friendsRecentAchievements", db.getFriendsRecentAchievements(acct.getID(), 10));
				request.setAttribute("friendsRecentCreatedQuizzes", db.getFriendsRecentlyCreatedQuizzes(acct.getID(), 10));
				
				//Popular quizzes
				request.setAttribute("popularQuizzes", db.getPopularQuizzes(acct.getID(), 10));
				
				//redirect them to the profile.jsp
				RequestDispatcher dpatch = request.getRequestDispatcher("profile.jsp");
				dpatch.forward(request, response);
				return;
			}
			
		} 
		if (id >= 0) {
			Account a = db.getAccount(id);
			request.setAttribute("recentResults", db.getRecentResultsForUser(a, 10));
			request.setAttribute("profileAccount", a);
			
			RequestDispatcher dpatch = request.getRequestDispatcher("view-activity.jsp");
			dpatch.forward(request, response);
			return;
		}
		
		request.setAttribute("error", "You're not logged in!  Please sign in to access your profile.");
			
		RequestDispatcher dpatch = request.getRequestDispatcher("login.jsp");
		dpatch.forward(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		RequestDispatcher dpatch = request.getRequestDispatcher("index.jsp");
		dpatch.forward(request, response);
	}

}
